package charlie.shop.warehouse;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import jxl.CellView;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
 * Provides write to excel functions
 * 
 * @author NoName
 */
public class WriteExcel {

    protected WritableCellFormat fontCaption;
    protected WritableCellFormat fontLabel;

    public WriteExcel() {
        try {
            // Lets create a times font
            WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
            // Define the cell format
            fontLabel = new WritableCellFormat(times10pt);
            // Lets automatically wrap the cells
            fontLabel.setWrap(true);

            // Create create a bold font with unterlines
            WritableFont times10ptBoldUnderline = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false,
                    UnderlineStyle.SINGLE);
            fontCaption = new WritableCellFormat(times10ptBoldUnderline);
            // Lets automatically wrap the cells
            fontCaption.setWrap(true);

            CellView cv = new CellView();
            cv.setFormat(fontLabel);
            cv.setFormat(fontCaption);
            cv.setAutosize(true);
        } catch (WriteException ex) {
            Logger.getLogger(WriteExcel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * write list of data into stream
     * 
     * @param data
     * @param stream
     * @throws IOException
     * @throws WriteException 
     */
    public void write(List data, OutputStream stream) throws IOException, WriteException {
        WorkbookSettings wbSettings = new WorkbookSettings();
        wbSettings.setLocale(new Locale("en", "EN"));

        WritableWorkbook workbook = Workbook.createWorkbook(stream, wbSettings);
        workbook.createSheet("Report", 0);
        WritableSheet excelSheet = workbook.getSheet(0);
        createLabel(excelSheet);
        writeContent(excelSheet, data);

        workbook.write();
        workbook.close();
    }

    
    protected void createLabel(WritableSheet sheet)
            throws WriteException {
    }
    /**
     * write line with data
     * 
     * @param sheet sheet to write
     * @param line line number
     * @param row current row
     * @throws WriteException 
     */
    protected void writeDataLine(WritableSheet sheet, int line, Object row) throws WriteException {
        addLabel(sheet, line + 1, 1, row.toString());
    }

    /**
     * write content into sheet
     * 
     * @param sheet sheet tow write
     * @param data data to write
     * @throws WriteException
     * @throws RowsExceededException 
     */
    protected void writeContent(WritableSheet sheet, List data) throws WriteException,
            RowsExceededException {
        for (int i = 0; i < data.size(); i++) {
            Object row = data.get(i);
            writeDataLine(sheet, i, row);
        }
    }

    /**
     * add caption method to sheet
     * 
     * @param sheet sheet to work
     * @param column column number
     * @param row row number
     * @param s text string
     * @throws RowsExceededException
     * @throws WriteException 
     */
    protected void addCaption(WritableSheet sheet, int column, int row, String s)
            throws RowsExceededException, WriteException {
        Label label;
        label = new Label(column, row, s, fontCaption);
        sheet.addCell(label);
    }

    /**
     * Add label to sheet
     * 
     * @param sheet sheet to write
     * @param column column number
     * @param row row number
     * @param s text string
     * @throws WriteException
     * @throws RowsExceededException 
     */
    protected void addLabel(WritableSheet sheet, int column, int row, String s)
            throws WriteException, RowsExceededException {
        Label label;
        label = new Label(column, row, s, fontLabel);
        sheet.addCell(label);
    }
}
